package com.qingmeng.outsourcing.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.qingmeng.outsourcing.mapper.*;
import com.qingmeng.outsourcing.model.*;
import com.qingmeng.outsourcing.portal.enums.FileTypeCode;
import com.qingmeng.outsourcing.portal.enums.ProjectScheduleCode;
import com.qingmeng.outsourcing.portal.model.dto.ProjectPublishParam;
import com.qingmeng.outsourcing.portal.model.vo.ProjectListVo;
import com.qingmeng.outsourcing.portal.service.ProjectService;
import com.qingmeng.outsourcing.portal.util.SecurityUtils;
import com.qingmeng.outsourcing.security.config.SecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    //项目表操作mapper
    @Autowired
    private ProjectMapper projectMapper;
    //项目与工程师类型操作mapper
    @Autowired
    private ProjectPositionTypeRelationMapper projectPositionTypeRelationMapper;
    //项目与项目类型关联操作mapper
    @Autowired
    private ProjectTypeRelationMapper projectTypeRelationMapper;
    //项目与进度关联操作mapper
    @Autowired
    private ProjectScheduleRelationMapper projectScheduleRelationMapper;
    //项目与项目文件关联操作mapper
    @Autowired
    private ProjectFilesMapper projectFilesMapper;
    //项目类型关联操作mapper
    @Autowired
    private ProjectTypeMapper projectTypeMapper;
    //职位类型关联操作mapper
    @Autowired
    private PositionTypeMapper positionTypeMapper;
    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 发布项目的实现
     * @param projectPublishParam 要发布项目的信息
     * @return 发布成功返回1，失败返回0，网络异常等其他问题，返回-1
     */
    @Transactional //在springBoot中开启事务，既操作的所有表，如果成功就都成功，有一个失败就都回滚
    @Override
    public int publishTask(ProjectPublishParam projectPublishParam) {
        int result = 0;

        //先获取project表里的数据
        Project project = new Project();
        //项目名称
        project.setProjectName(projectPublishParam.getProjectName());
        //项目简介
        project.setProjectShortIntro(projectPublishParam.getProjectIntroduction());
        //项目介绍
        project.setProjectIntroduction(projectPublishParam.getProjectIntroduction());
        //项目预算
        project.setProjectBudget(projectPublishParam.getProjectBudget());
        //项目周期
        project.setProjectDuration(projectPublishParam.getProjectDuration());
        //工作类型
        project.setWorkType(projectPublishParam.getWorkType());
        //工作内容
        project.setWorkExplain(projectPublishParam.getWorkExplain());

        //向project表中插入一条项目数据
        result = projectMapper.insertSelective(project);//会调用project.setId();
            if(result <= 0){
            return 0;
        }

        //获取前端传递过来的项目所需工程师ID列表
        List<Long> engineerTypeIdList= projectPublishParam.getEngineerTypeIdList();
        //第一个判断的是地址是否为空，第二个判断的是如果地址不为空里面的数据是否为空
        if(engineerTypeIdList != null && engineerTypeIdList.size() > 0){
            for (Long positionTypeId : engineerTypeIdList){
                ProjectPositionTypeRelation projectPositionTypeRelation = new ProjectPositionTypeRelation();
                projectPositionTypeRelation.setProjectId(project.getId());
                projectPositionTypeRelation.setPositionTypeId(positionTypeId);
                if(projectPositionTypeRelationMapper.insertSelective(projectPositionTypeRelation) <= 0){
                    return 0;
                }
            }
        }

        //获取前段传递过来的项目类型ID列表，自己写
        List<Long> projectTypeIdList = projectPublishParam.getProjectTypeIdList();
        //第一个判断的是地址是否为空，第二个判断的是如果地址不为空里面的数据是否为空
        if(projectTypeIdList != null && projectTypeIdList.size() > 0){
            for (Long positionId : projectTypeIdList){
                ProjectTypeRelation projectTypeRelation = new ProjectTypeRelation();
                projectTypeRelation.setProjectId(project.getId());
                projectTypeRelation.setProjectTypeId(positionId);
                if (projectTypeRelationMapper.insertSelective(projectTypeRelation) <= 0){
                    return 0;
                }
            }
        }

        //向数据库对应的表里插入项目进度信息，自己写
        ProjectScheduleRelation projectScheduleRelation = new ProjectScheduleRelation();
        projectScheduleRelation.setProjectId(project.getId());
        projectScheduleRelation.setScheduleId(ProjectScheduleCode.PUBLISHED);
        if(projectScheduleRelationMapper.insertSelective(projectScheduleRelation) <= 0){
            return 0;
        }

        //获取前段传递过来的项目文件列表
        List<ProjectFiles> fileList = projectPublishParam.getFilesList();
        if(fileList != null && fileList.size() > 0)
        {
            for (int i = 0; i<fileList.size();i++){
                ProjectFiles projectFiles = new ProjectFiles();
                projectFiles.setProjectId(project.getId());
                projectFiles.setFileUrl(fileList.get(i).getFileUrl());
                projectFiles.setFileName(fileList.get(i).getFileName());
                projectFiles.setFileTypeId(fileList.get(i).getFileTypeId());
                projectFiles.setUserId(SecurityUtils.getCurrentUser().getId());
                projectFiles.setCreateTime(fileList.get(i).getCreateTime());
                if(projectFilesMapper.insertSelective(projectFiles) <= 0){
                    return 0;
                }
            }
        }

        return result;
    }
    /*  自己写的  可以实现   但是现在已经将ProjectListVo类稍作了修改，
        既 将项目类型Id列表和工程师Id列表的类型 油List<Long> 改为了 List<ProjectType>
        也就是不返回索引而是将整个类型对象都返回过来
        想要实现还需要将类型改回来

    @Transactional
    @Override
    public List<ProjectListVo> list(int pageNum,int pageSize) {
        List<ProjectListVo> projectListVos = new ArrayList<>();
        //取出项目表，根据每个项目进行插入数据
        ProjectExample projectExample = new ProjectExample();
        PageHelper.startPage(pageNum,pageSize);
        List<Project> projects = projectMapper.selectByExample(projectExample);
        for (Project project : projects){
            ProjectListVo projectListVo = new ProjectListVo();
            projectListVo.setProjectName(project.getProjectName());
            projectListVo.setProjectShortIntro(project.getProjectShortIntro());
            projectListVo.setProjectDuration(project.getProjectDuration());
            projectListVo.setProjectBudget(project.getProjectBudget());
            projectListVo.setProjectIntroduction(project.getProjectIntroduction());
            projectListVo.setWorkType(project.getWorkType());
            projectListVo.setWorkExplain(project.getWorkExplain());
            //根据项目id查询出项目类型id列表
            ProjectTypeRelationExample projectTypeRelationExample = new ProjectTypeRelationExample();
            projectTypeRelationExample.createCriteria().andProjectIdEqualTo(project.getId());
            List<ProjectTypeRelation> projectTypeRelations = projectTypeRelationMapper.selectByExample(projectTypeRelationExample);
            for(ProjectTypeRelation projectTypeRelation : projectTypeRelations){
                Long projectId = projectTypeRelation.getProjectId();
                Long projectTypeId = projectTypeRelation.getProjectTypeId();
                List<Long> projectTypeList = new ArrayList<>();
                projectTypeList.add(projectId);
                projectTypeList.add(projectTypeId);
                projectListVo.setProjectTypeIdList(projectTypeList);
            }
            projectListVos.add(projectListVo);
        }
        return projectListVos;
    }
     */

    //老师写的

    @Transactional
    @Override
    public List<ProjectListVo> list(int pageNum, int pageSize) {
        //声明一个返回值对象
        List<ProjectListVo> projectListVos = new ArrayList<>();
        //分页插件只会对他下面第一个sql有效
        PageHelper.startPage(pageNum,pageSize);
        //根据projectExample来获取数据库里的project数据
        ProjectExample projectExample = new ProjectExample();
        List<Project> projectList = projectMapper.selectByExample(projectExample);
//        int tag = 0;
        for (Project project : projectList){
            ProjectListVo projectListVo = new ProjectListVo();
            Long projectId = project.getId();

            //把表里有的数据都加入返回值里
            projectListVo.setProjectId(projectId);
            projectListVo.setProjectName(project.getProjectName());
            projectListVo.setProjectShortIntro(project.getProjectShortIntro());
            projectListVo.setProjectDuration(project.getProjectDuration());
            projectListVo.setProjectBudget(project.getProjectBudget());
            projectListVo.setProjectIntroduction(project.getProjectIntroduction());
            projectListVo.setWorkType(project.getWorkType());
            projectListVo.setWorkExplain(project.getWorkExplain());

            //项目类型
            ProjectTypeRelationExample projectTypeRelationExample = new ProjectTypeRelationExample();
            projectTypeRelationExample.createCriteria().andProjectIdEqualTo(projectId);
            List<ProjectTypeRelation> projectTypeRelations = projectTypeRelationMapper.selectByExample(projectTypeRelationExample);
//            //List<ProjectType> projectTypeList = new ArrayList<>();
            List<ProjectType> projectTypeList = new ArrayList<>();
            for (ProjectTypeRelation projectTypeRelation : projectTypeRelations){
//                ProjectTypeExample projectTypeExample = new ProjectTypeExample();
//                projectTypeExample.createCriteria().andIdEqualTo(projectTypeRelation.getProjectTypeId());
//                ProjectType projectType = projectTypeMapper.selectByExample(projectTypeExample);
                //这里不可以使用selectByExample 而是 使用 selectByPrimaryKey   因为byExample返回的是List类型 而byPrimaryKey返回的才是当个对象
                ProjectType projectType = projectTypeMapper.selectByPrimaryKey(projectTypeRelation.getProjectTypeId());
                projectTypeList.add(projectType);

//                //projectListVos.get(tag).setProjectTypeIdList(projectTypeList);
//                  ProjectType projectType = projectTypeMapper.selectByPrimaryKey(projectTypeRelation.getProjectTypeId());
//                  //projectTypeList.add(projectType);
//                projectListVo.setProjectTypeIdList(projectTypeList);
            }
            projectListVo.setProjectTypeIdList(projectTypeList);
//            tag++;
            //职位类型
            ProjectPositionTypeRelationExample projectPositionTypeRelationExample = new ProjectPositionTypeRelationExample() ;
            projectPositionTypeRelationExample.createCriteria().andProjectIdEqualTo(projectId);
            List<ProjectPositionTypeRelation> projectPositionTypeRelations = projectPositionTypeRelationMapper.selectByExample(projectPositionTypeRelationExample);
            List<PositionType> positionTypes = new ArrayList<>();
            for(ProjectPositionTypeRelation projectPositionTypeRelation : projectPositionTypeRelations){
                PositionType positionType = positionTypeMapper.selectByPrimaryKey(projectPositionTypeRelation.getPositionTypeId());
                positionTypes.add(positionType);
            }
            projectListVo.setEngineerTypeIdList(positionTypes);

            //文件类型
            ProjectFilesExample projectFilesExample = new ProjectFilesExample();
            projectFilesExample.createCriteria().andProjectIdEqualTo(projectId);
            List<ProjectFiles> projectFiles = projectFilesMapper.selectByExample(projectFilesExample);
            projectListVo.setFilesList(projectFiles);

            //项目进度
            ProjectScheduleRelationExample projectScheduleRelationExample = new ProjectScheduleRelationExample();
            projectScheduleRelationExample.createCriteria().andProjectIdEqualTo(projectId);
            List<ProjectScheduleRelation> projectScheduleRelations = projectScheduleRelationMapper.selectByExample(projectScheduleRelationExample);
            List<Schedule> schedules = new ArrayList<>();
            for (ProjectScheduleRelation projectScheduleRelation : projectScheduleRelations){
                Schedule schedule = scheduleMapper.selectByPrimaryKey(projectScheduleRelation.getScheduleId());
                schedules.add(schedule);
            }
            projectListVo.setProjectSchedule(schedules);
            projectListVos.add(projectListVo);
        }
        return projectListVos;
    }
}
